<div avb-busy overlay ng-if="loading && !intally"></div>

<div class="dashboard">
    <h2>
      <span ng-i18next>avAdmin.sidebar.dashboard</span>
      <span class="text-gray-light" ng-if="election.title">{{election.title | truncate:45}}</span>
    </h2>

    <!-- dashboard status bar -->
    <div class="statusbar row text-center">
        <div class="col-xs-2" ng-class="{true:'text-success', false:'text-muted'}[index > $index]" ng-repeat="status in statuses">
            <div class="stick" ng-class="{'stickini': $first, 'stickend': $last}">
                <i class="fa fa-cog fa-spin fa-sw fa-2x text-warning" ng-if="index == $index && intally"></i>
                <i class="fa fa-sw fa-2x fa-check-circle" ng-if="index > $index"></i>
                <i class="fa fa-sw fa-2x fa-circle" ng-if="index == $index && !intally"></i>
                <i class="fa fa-sw fa-2x fa-circle" ng-if="index < $index"></i>
            </div>
            <div class="small" ng-i18next ng-if="index != $index">{{ status }}</div>
            <button ng-click="doActionConfirm($index)" class="actionbtn btn btn-success" ng-if="index == $index && nextaction">
                <div class="small" ng-i18next>{{ nextaction }}</div>
                <strong ng-i18next>avAdmin.dashboard.donow</strong>
            </button>
            <button class="btn btn-warning" ng-if="index == $index && intally">
                <div class="small" ng-i18next="avAdmin.dashboard.doingtally"></div>
            </button>
        </div>
    </div>
    <!-- endof dashboard status bar -->

    <div ng-if="error" class="bg-danger">
        <span ng-if="error.payload">{{ error.payload }}</span>
        <span ng-if="!error.payload">{{ error }}</span>
    </div>

    <div ng-if="msg" class="bg-success">
        <span ng-i18next="{{ msg }}"></span>
    </div>

    <!-- election info -->
    <div class="row info">
        <div class="col-sm-4 text-right">
            <div class="block">
                <strong>{{ election.votes|number }}</strong><br/>
                <span ng-i18next>avAdmin.dashboard.votes</span>
            </div>
        </div>
        <div class="col-sm-4 text-right">
            <div class="block">
                <strong>{{ election.auth.census|number }}</strong><br/>
                <button
                  ng-click="sendAuthCodesModal(election)"
                  ng-if="election.status == 'started'"
                  class="btn btn-success send-auth pull-left">
                    <i class="fa fa-sw fa-send"></i>
                    <span ng-i18next>
                      avAdmin.dashboard.sendAuthCodes
                    </span>
                </button>
                <span ng-i18next>avAdmin.dashboard.census</span>
            </div>
        </div>
        <div class="col-sm-4 text-right">
            <div class="block">
                <span ng-i18next>avAdmin.dashboard.questions</span>
                <strong>{{ election.questions.length }}</strong><br/>
                <span ng-i18next>avAdmin.dashboard.answers</span>
                <strong>{{ election.answers }}</strong>
            </div>
        </div>
    </div>
    <!-- endof election info -->

    <!-- general info -->
    <div class="row general">
        <div class="col-md-6">
            <div class="well">
                <dl class="dl-horizontal">
                  <dt ng-i18next>avAdmin.dashboard.status</dt>
                  <dd ng-i18next="{{ election.status }}"></dd>
                  <dt ng-i18next>avAdmin.dashboard.authentication</dt>
                  <dd>{{ election.auth.authentication }}</dd>
                  <dt ng-i18next>avAdmin.dashboard.electionid</dt>
                  <dd>{{ election.id }}</dd>
                </dl>
            </div>

            <div class="well text-right">
                <a
                  ui-sref="election.public.show.home({ id: election.id })"
                  target="_blank"
                  ng-i18next>
                  avAdmin.dashboard.electionPublic
                </a>
                <br/>
                <a
                  ui-sref="election.booth-nohmac({ id: election.id })"
                  target="_blank"
                  ng-i18next>
                    avAdmin.dashboard.preview
                </a>
                <!--
                <a href="">Embed election</a>
                -->
            </div>

            <button ng-click="doActionConfirm(index)" class="btn btn-block btn-lg btn-success" ng-if="nextaction" ng-i18next="{{ nextaction }}"></button>

            <button
              ng-click="duplicateElection()"
              class="btn btn-block btn-lg btn-success"
              ng-i18next>
              avAdmin.dashboard.duplicateElection
            </button>

            <button class="btn btn-block btn-lg btn-warning" ng-if="intally">
                <i class="fa fa-cog fa-spin fa-sw fa-2x"></i><br/>
                <span ng-i18next="avAdmin.dashboard.doingtally"></span>
            </button>
        </div>
        <!-- authorities -->
        <div class="col-md-6">
            <div class="well">
                <h4>
                    <span ng-i18next>avAdmin.dashboard.authorities</span>
                    <span>{{ election.auths.length}}</span>
                </h4>

                <ul class="list-group">
                    <li class="authority list-group-item" ng-repeat="auth in election.auths">
                        <span class="badge" ng-if="auth == election.director"><i class="fa fa-sitemap"></i></span>
                        {{ auth }}
                    </li>
                </ul>
            </div>
        </div>
    </div>
    <!-- endof general -->

    <!-- results -->
    <h2 ng-if="election.results.questions" ng-i18next>avAdmin.dashboard.results</h2>
    <div class="well question-result" ng-repeat="q in election.results.questions">
        <div class="row">
            <div class="col-md-7">
                <h2>{{ q.title }}</h2>
            </div>
            <div class="col-md-5">
                <dl class="dl-horizontal">
                  <dt>{{ (q.totals.valid_votes + q.totals.null_votes + q.totals.blank_votes) }}</dt>
                  <dd ng-i18next>avAdmin.dashboard.votes</dd>
                  <dt></dt>
                  <dd>--</dd>
                  <dt>{{ q.totals.valid_votes|number }}</dt>
                  <dd>
                      <span ng-i18next>avAdmin.dashboard.optionvotes</span>
                      <span>({{ percentVotes(q.totals.valid_votes, q) }})</span>
                  </dd>
                  <dt>{{ q.totals.blank_votes }}</dt>
                  <dd>
                      <span ng-i18next>avAdmin.dashboard.blankvotes</span>
                      <span>({{ percentVotes(q.totals.blank_votes, q) }})</span>
                  </dd>
                  <dt>{{ q.totals.null_votes }}</dt>
                  <dd>
                      <span ng-i18next>avAdmin.dashboard.nullvotes</span>
                      <span>({{ percentVotes(q.totals.null_votes, q) }})</span>
                  </dd>
                </dl>
            </div>
        </div>

        <table role="table" class="table">
            <tr class="text-success">
                <th></th>
                <th ng-i18next>avAdmin.dashboard.option</th>
                <th ng-i18next>avAdmin.dashboard.votes</th>
                <th></th>
                <th ng-i18next>avAdmin.dashboard.winner</th>
            </tr>
            <tr ng-repeat="an in q.answers|orderBy:'-total_count'">
                <td>{{ $index + 1 }}</td>
                <td>{{ an.text }}</td>
                <td>{{ an.total_count|number }}</td>
                <td>{{ percentVotes(an.total_count, q, "over-valid-votes") }}</td>
                <td ng-if="an.winner_position === 0 || an.winner_positon">{{ an.winner_positon + 1}}</td>
                <td ng-if="!(an.winner_position === 0 || an.winner_positon)">--</td>
            </tr>
        </table>
    </div>
    <!-- endof results -->
</div>
